from datetime import datetime

import jwt
from django.conf import settings
from django.db import models

from user_center.const import Sex, UserLogEvent


class CampusIdentity(models.Model):
    campus_name = models.CharField(max_length=256, verbose_name="高校名称")
    major = models.CharField(max_length=256, verbose_name="专业")
    class_name = models.CharField(max_length=256, verbose_name="班级名称")
    admission_date = models.DateField(verbose_name="入学日期")
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        verbose_name = "高校身份认证"
        verbose_name_plural = "高校身份认证"

    def __str__(self):
        return self.campus_name


class User(models.Model):
    username = models.CharField(
        max_length=150, unique=True, default="", verbose_name="用户名"
    )
    personal_profile = models.CharField(max_length=512, default="", verbose_name="个人简介")
    sex = models.SmallIntegerField(
        choices=Sex.choices, default=Sex.SECRET, verbose_name="性别"
    )
    password = models.CharField(max_length=128, verbose_name="密码")
    email = models.EmailField(max_length=256, verbose_name="邮箱")
    avatar = models.CharField(
        max_length=256,
        default="https://caibo-0520.oss-cn-shenzhen.aliyuncs.com/avatar/default.png",
        verbose_name="头像",
    )
    birthday = models.DateTimeField(null=True, verbose_name="生日")
    campus_identity = models.ForeignKey(
        CampusIdentity, null=True, on_delete=models.PROTECT, verbose_name="高校身份认证"
    )
    is_deleted = models.BooleanField(default=False, verbose_name="已删除")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    @property
    def token(self):
        return self._generate_jwt_token()

    def _generate_jwt_token(self):
        token = jwt.encode(
            {
                "exp": datetime.utcnow() + settings.TOKEN_EXPIRE_TIME,
                "iat": datetime.utcnow(),
                "data": {
                    "user_id": self.pk,
                    "username": self.username,
                    "email": self.email,
                },
            },
            settings.SECRET_KEY,
            algorithm="HS256",
        )

        return token.encode("utf-8").decode("utf-8")

    class Meta:
        verbose_name = "用户"
        verbose_name_plural = "用户"

    def __str__(self):
        return self.username


class UserLog(models.Model):
    user = models.ForeignKey(
        User, on_delete=models.PROTECT, verbose_name="用户", related_name="logs"
    )
    event = models.SmallIntegerField(choices=UserLogEvent.choices, verbose_name="事件")
    detail = models.CharField(max_length=256, null=True, verbose_name="日志详情")
    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")

    class Meta:
        verbose_name = "用户日志"
        verbose_name_plural = "用户日志"
